/**
 * 用setTimeout模拟setTimeInterval
 * @param {*} func 回调函数
 * @param {*} timeout 延时/频率，类似setTimeInterval的频率
 * @returns 取消的定时器的函数
 */

const simulateSetInterval = (func, timeout) => {
  let timer = null
  const interval = () => {
    timer = setTimeout(() => {
      func()
      interval()
    }, timeout)
  }

  interval()
  return () => clearTimeout(timer)
}

const cancel = simulateSetInterval( () => {
  console.log(1)
}, 100)

setTimeout(() => {
  cancel()
}, 1000)
